package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.basic.context.ContextUtil;
import top.tangyh.lamp.care.mapper.BDocRecordMapper;
import top.tangyh.lamp.care.service.BDocRecordService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BDocRecordManager;
import top.tangyh.lamp.care.entity.BDocRecord;
import top.tangyh.lamp.care.vo.save.BDocRecordSaveVO;
import top.tangyh.lamp.care.vo.update.BDocRecordUpdateVO;
import top.tangyh.lamp.care.vo.result.BDocRecordResultVO;
import top.tangyh.lamp.care.vo.query.BDocRecordPageQuery;
import top.tangyh.lamp.common.utils.ConvertUtil;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 文书记录表
 * </p>
 *
 * @author zqb
 * @date 2024-07-29 14:16:16
 * @create [2024-07-29 14:16:16] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BDocRecordServiceImpl extends SuperServiceImpl<BDocRecordManager, Long, BDocRecord> implements BDocRecordService {

    private final BDocRecordMapper bDocRecordMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doSave(BDocRecordSaveVO yxDocRecordVO) {
        BDocRecord po = ConvertUtil.convert(yxDocRecordVO, BDocRecord.class);
        if(null != yxDocRecordVO.getRecordTime()){
            po.setRecordTime(yxDocRecordVO.getRecordTime());
        }
        if(null == yxDocRecordVO.getId()){
            BDocRecord yxDocRecordSavePO = ConvertUtil.convert(yxDocRecordVO, BDocRecord.class);
            yxDocRecordSavePO.setCreatedTime(LocalDateTime.now());
            yxDocRecordSavePO.setUpdatedTime(LocalDateTime.now());
            yxDocRecordSavePO.setCreatedBy(ContextUtil.getUserId());
            yxDocRecordSavePO.setUpdatedBy(ContextUtil.getUserId());
            bDocRecordMapper.insert(yxDocRecordSavePO);
        }
        else{
            updateById(po);
        }
    }


    @Override
    public List<BDocRecordResultVO> queryAll(String patientIdent, Integer type, String wardIdent, Long startTime, Long endTime) {
        Date start = null;
        Date end = null;
        if(null != startTime){
            start = new Date(startTime);
        }
        if(null != endTime){
            end = new Date(endTime);
        }
        List<BDocRecord> list = bDocRecordMapper.queryAll(patientIdent,type,wardIdent,start,end);
        if(CollectionUtil.isNotEmpty(list)){
            return ConvertUtil.convertList(list,BDocRecordResultVO.class);
        }
        return new ArrayList<BDocRecordResultVO>();
    }

    @Override
    public BDocRecordResultVO detialListOne(String patientIdent, Integer type) {
        List<BDocRecord> list = bDocRecordMapper.queryOne(patientIdent,type);
        if(CollectionUtil.isEmpty(list)){
            return null;
        }
        else{
            return ConvertUtil.convert(list.get(0),BDocRecordResultVO.class);
        }
    }



}


